package com.bz.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bz.common.Codeenum;
import com.bz.common.PageInfo;
import com.bz.common.PageUtil;
import com.bz.common.ResponseDto;
import com.bz.entity.PreojectEntity;
import com.bz.service.IPreojectService;

@RestController
@RequestMapping(value = "/admin/preoject")
public class PreojectController {

    @Autowired
    private IPreojectService preojectService;

    @RequestMapping(value = "/list")
    public PageInfo list(@RequestParam(required = false) String name,
                         @RequestParam(required = false) String areaId,
                         @RequestParam(required = false) Long pageNo) {

        PageInfo page = PageUtil.getPageInfo(pageNo, PageUtil.pageSize);
        LambdaQueryWrapper<PreojectEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(name)) {
            queryWrapper.like(PreojectEntity::getName, name);
        }
        if (StringUtils.isNotEmpty(areaId)) {
            queryWrapper.eq(PreojectEntity::getAreaId, areaId);
        }
        return preojectService.list(page, queryWrapper);
    }

    @RequestMapping(value = "/add")
    public ResponseDto<String> add(@RequestBody @Valid PreojectEntity data) {
        preojectService.add(data);
        return new ResponseDto<>(Codeenum.SUCCESS.getCode(), "添加成功");

    }

    @RequestMapping(value = "/update")
    public ResponseDto<String> update(@RequestBody @Valid PreojectEntity data) {
        preojectService.update(data);
        return new ResponseDto<>(Codeenum.SUCCESS.getCode(), "修改成功");
    }

    @RequestMapping(value = "/delete")
    public ResponseDto<String> delete(@RequestParam(required = true) Long id) {

        preojectService.delete(id);
        return new ResponseDto<>(Codeenum.SUCCESS.getCode(), "删除成功");
    }
}
